home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11969 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.2 KB  |  138 lines

  1. Newsgroups: comp.lang.c++
  2. Path: lion.cs.latrobe.edu.au!boylesgj
  3. From: boylesgj@lion.cs.latrobe.edu.au (Gregary J Boyles)
  4. Subject: Class constructor usage within another class constructor problem!
  5. X-Nntp-Posting-Host: lion.cs.latrobe.edu.au
  6. Message-ID: <DoEv3x.IDL@latcs1.lat.oz.au>
  7. Sender: news@latcs1.lat.oz.au (news)
  8. Organization: Comp.Sci & Comp.Eng, La Trobe Uni, Australia
  9. Date: Sun, 17 Mar 1996 12:09:33 GMT
  10.  
  11. See astericks for problems.
  12.  
  13.  // address.cpp
  14.  
  15.  #include "address.h"
  16.  
  17.  // Constructor
  18.  Address::Address(int ANumber,const char *AStreet,const char *ACity,int AZip)
  19.  {
  20.       Number=ANumber;
  21.  
  22.       // *******************************************************************
  23.       // The next two lines create objects of class String. Do these objects
  24.       // get placed into the Street and City members of class Address, i.e.
  25.       // are these lines equivelent to Address.Street=AStreet etc or have I
  26.       // got it wrong?
  27.       // *******************************************************************
  28.       String Street(AStreet);
  29.       String City(ACity);
  30.       Zip=AZip;
  31.  }
  32.  
  33.  // Copy constructor
  34.  Address::Address(Address& AnAddress)
  35.  {
  36.       Number=AnAddress.Number;
  37.  
  38.       // **********************************************************************
  39.       // Can't pass AnAddress.Street by reference i.e. AnAddress.Street & as 
  40.       // it generates a compile error. Why?
  41.       // **********************************************************************
  42.       String Street(AnAddress.Street);
  43.  
  44.       // *******************************************************************
  45.       // Can't pass AnAddress.City by reference i.e. AnAddress.City &. Why?
  46.       // *******************************************************************
  47.       String City(AnAddress.City);
  48.       Zip=AnAddress.Zip;
  49.  }
  50.  
  51.  // Deconstructor
  52.  Address::~Address()
  53.  {
  54.       Number=0;
  55.       String Street("");
  56.       String City("");
  57.       Zip=0;
  58.  }
  59.  
  60.  // address.h
  61.  
  62.  #ifndef __ADDRESS_H
  63.  #define __ADDRESS_H
  64.  #define STR_STREET " street"
  65.  
  66.  #include "strclass.h"
  67.  #include "defines.h"
  68.  
  69.  class Address
  70.  {
  71.       private : int Number;
  72.             String Street;
  73.             String City;
  74.             int Zip;
  75.  
  76.       public : // Constructors
  77.            Address(int ANumber,const char *AStreet,const char *ACity,int Zip);
  78.            Address();
  79.            // Copy constructor
  80.            Address(Address& AnAddress);
  81.            // Deconstructor
  82.            ~Address();
  83.  }
  84.  
  85.  #endif
  86.  
  87.  // strclass.cpp
  88.  
  89.  #include "strclass.h" 
  90.  #include "string.h"
  91.  
  92.  // Constructor
  93.  String::String(const char *AString)
  94.  {
  95.       Length=strlen(AString)+1;
  96.       TheString=new char[Length];
  97.       strcpy(TheString,AString);
  98.  }
  99.  
  100.  // Copy constructor
  101.  String::String(const String& SourceString)
  102.  {
  103.       Length=SourceString.Length;
  104.       TheString=new char[Length];
  105.       strcpy(TheString,SourceString.TheString);
  106.  }
  107.  
  108.  // Deconstructor
  109.  String::~String()
  110.  {
  111.       delete TheString;
  112.  }
  113.  
  114.  // strclass.h
  115.  
  116.  #ifndef __STRCLASS_H
  117.  #define __STRCLASS_H
  118.  
  119.  #include <iostream.h>
  120.  #include "defines.h"
  121.  
  122.  class String
  123.  {
  124.      int Length;
  125.      char  *TheString;
  126.  
  127.      public : // Constructor
  128.           String(const char *AString="");
  129.           // Copy constructor
  130.           String(const String&);
  131.           // Destructor
  132.           ~String();
  133.  
  134.  };
  135.  
  136.  #endif 
  137.  
  138.